package com.lv.service.room.book

import android.content.Context
import com.lv.service.repo.BookSearch
import com.lv.service.repo.BookSearchDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * cniao数据库操作帮助类
 */
object BookSearchDbHelper {

    /**
     * 获取room数据表中存储的userInfo
     * return liveData形式
     */
    fun getLiveBooksSearch(context: Context, search: String) =
        BookSearchDatabase.getInstance(context).bookSearchDao.queryLiveBooksSearch(search)

    /**
     * 以普通数据对象的形式，获取userInfo
     */
    fun getBooksSearch(context: Context, search: String) =
        BookSearchDatabase.getInstance(context).bookSearchDao.queryBooksSearch(search)

    /**
     * 删除数据表中的userInfo信息
     */
    fun deleteBooksSearch(context: Context, search: String) {
        GlobalScope.launch(Dispatchers.IO) {
            getBooksSearch(context, search = search)?.let { info ->
                BookSearchDatabase.getInstance(context).bookSearchDao.deleteBooksSearch(info)
            }
        }
    }

    /**
     * 删除数据表中的userInfo信息
     */
    fun deleteAllBooksSearch(context: Context) {
        GlobalScope.launch(Dispatchers.IO) {
            BookSearchDatabase.getInstance(context).bookSearchDao.deleteAllBooksSearch()

        }
    }

    /**
     * 新增用户数据到数据表
     */
    fun insertBooksSearch(context: Context, bookSearch: BookSearch) {
        GlobalScope.launch(Dispatchers.IO) {
            BookSearchDatabase.getInstance(context).bookSearchDao.insertBooksSearch(bookSearch)
        }
    }

    /**
     * 获取的所有用户数据到数据表
     */
    suspend fun getAllBooksSearch(context: Context) =
        BookSearchDatabase.getInstance(context).bookSearchDao.getAllBooksSearch()


}